import { ElMessage } from 'element-plus'
import Logger from "../../../utils/logger";

export default function validateMagnet(e, graph) {
  Logger.log('验证Magnet...')
  const cellData = e.cell.getData()
  let shape
  if (cellData.inheritFromNode) {
    shape = cellData.inheritFromNode
  } else {
    shape = e.cell.shape
  }

  const rule = validateMagnetRule[shape]

  if (rule) {
    return validateMagnetRule[shape](e, graph)
  }

  return true
}

export const validateMagnetRule = {
  start(e, graph) {
    const outgoingEdges = graph.getOutgoingEdges(e.cell) || []
    if (outgoingEdges.length) {
      ElMessage('开始节点仅允许拉出 1 条线')
      return false
    }

    return true
  },

  condition(e, graph) {
    const outgoingEdges = graph.getOutgoingEdges(e.cell) || []

    if (outgoingEdges.length > 1) {
      ElMessage('条件节点仅允许拉出 2 条线')
      return false
    }

    return true
  },

  switcherCondition(e, graph) {
    const outgoingEdges = graph.getOutgoingEdges(e.cell) || []

    if (outgoingEdges.length>9) {
      ElMessage('分支节点仅允许拉出 1 - 10 条线')
      return false
    }

    return true
  },

  switcherConcurrent(e, graph) {
    return this.switcherCondition(e, graph)
  },

  abtest(e, graph) {
    ElMessage('ABTEST节点组件修改实验组后会自动创建连接线')
    return false
  },

  wait(e, graph) {
    const outgoingEdges = graph.getOutgoingEdges(e.cell) || []
    if (outgoingEdges.length > 0) {
      ElMessage('等待节点仅允许拉出 1 条线')
      return false
    }

    return true
  },

  end(e, graph) {
    ElMessage('结束节点不允许向外拉线')
    return false
  },

  action(e, graph) {
    const outgoingEdges = graph.getOutgoingEdges(e.cell) || []
    if (outgoingEdges.length) {
      ElMessage('动作节点仅允许创建一条出线')
      return false
    }
    return true
  },

  event(e, graph) {
    const outgoingEdges = graph.getOutgoingEdges(e.cell) || []
    if (outgoingEdges.length) {
      ElMessage('事件节点仅允许创建一条出线')
      return false
    }
    return true
  },

  placeholder() {
    ElMessage('占位节点不允许向外拉线')
    return false
  }
}
